// pages/wuliurongqiguanli/nav_check_gzcbl/check_gzcbl.js
const mybehavior = require("../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    material_code: "",
    company: "",
    list: [],
    options1: [],
    options11: [],
    popup_show1: false,
    baseUrl: "https://logisticsengineering.gree.com/pfep2_api",
    search_value: "",
    radio: "",
    radio_show: true,

    leibie: "",
    popup_show11: false,
    popup_show2: false,
    options111: ['来料包装库'],
    radio11: '',
    checkbox_list1: [

    ],
    checkbox_value1: "",
    checkbox_list2: [

    ],
    checkbox_value2: "",
    batch_no: "",
    previewImageClick: false


  },
  onSearch1(e) {
    console.log(e.detail);

    if (e.detail == "") {
      let arr = this.data.options11
      this.setData({
        options1: arr
      })
    } else {
      let list1 = this.data.options11
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        options1: arr
      })
    }
  },
  // 点击公司名称选取输入框
  onClick1(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show1: true
    })
  },
  // 选择公司名称输入框关闭
  onClose1() {
    this.setData({ popup_show1: false });
  },
  // 确认公司名称值--弹出框关闭
  onChange1(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      company: value,
      popup_show1: false,
    })
  },
  // 获取--供应商--数据
  getPermissionDatas() {
    let url = this.data.baseUrl + "/data_auth_manage/get_u_permission"
    let data = {

    }
    this.post(url, data).then(res => {
      // console.log("获取供应商数据", res.data)
      let data_list = Object.keys(res.data)
      // console.log("xxx", data_list);

      if (res.code == 20000) {

        this.setData({
          options1: data_list,
          options11: data_list,
          previewImageClick: true

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  onRadioChange() {
    // 批次号转物料编码
    let url = this.data.baseUrl + "/standard_doc_store_manage/transfer_batch2material_num "
    let data = {
      batch_num: this.data.batch_no
    }
    this.post(url, data).then(res => {
      console.log("批次号转物料编码", res.data)

      if (res.code == 20000) {
        this.getMaterialCodeDatas(res.data.PARTCODE)
        this.setData({
          material_code: res.data.PARTCODE
        })


      } else {
        // wx.showToast({
        //   title: res.msg,
        //   icon: 'error'
        // })
      }
    }).catch(err => {
      console.log(err);
      // wx.showToast({
      //   title: err.msg,
      //   icon: 'error'
      // })
    })


  },
  // 扫描
  scan1(e) {
    let key = e.currentTarget.dataset.key
    console.log(key);
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      console.log(label_code);
      setTimeout(function () {
        wx.hideLoading()
        self.setData({
          [key]: label_code,
        })
        if (key == "material_code") {
          self.getMaterialCodeDatas1()
        } else {
          self.onRadioChange()
        }
      }, 500)

    }).catch(err => {
      console.log(err);
    });
  },
  // submit() {

  //   let val = this.data.material_code
  //   wx.navigateTo({
  //     url: '/pages/pfep/safeguard_select/safeguard_select?material_num=' + val,
  //   })

  // },
  // 点击标准类别选取输入框
  onClick11(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show11: true
    })
  },
  // 选择标准类别选取输入框关闭
  onClose11() {
    this.setData({ popup_show11: false });
  },
  // 确认标准类别选取值--弹出框关闭
  onPopupChange1(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      leibie: value,
      popup_show11: false,
    })
    this.getTemplateDatas()
    this.getStandardDatas()
  },
  // 选择模板方式
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
    let key = event.detail
    if (key == '1') {
      this.setData({
        popup_show2: true,
      })
    } else if (key == '2') {
      this.setData({
        popup_show3: true,
      })
    } else {
      console.log("123");
      wx.navigateTo({
        url: '/pages/pfep/MaintenanceTemplate/Template3/Template3?citg=' + this.data.citg + '&cpcl=' + this.data.cpcl + '&material_num=' + this.data.material_code,
        // success(res) {
        //     res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.row_data)

        // }
      })
    }
  },
  submit() {
    if (this.data.radio == "3") {
      console.log(this.data.radio);
      // wx.navigateTo({
      //     url: '/pages/pfep/MaintenanceTemplate/Template1/Template1',
      // })
      wx.navigateTo({
        url: '/pages/pfep/MaintenanceTemplate/Template3/Template3?citg=' + this.data.citg + '&cpcl=' + this.data.cpcl + '&material_num=' + this.data.material_num,
        // success(res) {
        //     res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.row_data)

        // }
      })

    }
  },
  // 筛选现有包装模板数据
  bindInput1(e) {
    console.log(e.detail);

  },
  // 获取--现有包装模板--数据
  getTemplateDatas() {
    let query_cond = {
      name: { "$regex": this.data.citg }
    }

    let url = this.data.baseUrl + "/template_manage/query_templates"
    let data = {
      page: 1,
      page_size: 99999,
      query_cond: query_cond
    }
    this.post(url, data).then(res => {
      console.log("现有包装模板", res.data.results);

      if (res.code == 20000) {

        this.setData({
          checkbox_list1: res.data.results,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  query_cond: {
    "content.database": "辅材库"
  },
  // 获取--物料编码模板--数据
  getStandardDatas() {

    let query_cond = {
      "content.infoData.material_code": { "$regex": this.data.material_code },
    }


    let url = this.data.baseUrl + "/standard_doc_store_manage/query_standard_doc_store"
    let data = {
      page: 1,
      page_size: 99999,
      query_cond: query_cond,
      tb_key: ""
    }
    console.log("afsdf", data);
    this.post(url, data).then(res => {
      console.log("物料编码模板", res.data.results);

      if (res.code == 20000) {

        this.setData({
          checkbox_list2: res.data.results,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },


  // 现有包装模板值选中
  onPopupChange2(event) {
    console.log(event);
    this.setData({
      checkbox_value1: event.detail,
      // popup_show2: false,
    })


  },
  onClose2() {
    this.setData({ popup_show2: false });
  },
  // 预览包装信息
  previewBtn1(e) {
    console.log(e.currentTarget.dataset.row_data);
    // wx.navigateTo({
    //     url: '/pages/pfep/MaintenanceTemplate/Template1/Template1',
    // })
    wx.navigateTo({
      url: '/pages/pfep/MaintenanceTemplate/previewPage/previewPage1/previewPage1?citg=' + this.data.citg + '&cpcl=' + this.data.cpcl + '&material_num=' + this.data.material_code,
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.row_data)

      }
    })
  },



  // 现有物料编码标准值选中
  onPopupChange3(event) {
    this.setData({
      checkbox_value2: event.detail,
      // popup_show2: false,
    })
  },
  onClose3() {
    this.setData({ popup_show3: false });
  },
  toTemplate1(e) {
    console.log(e.currentTarget.dataset.row_data);
    // wx.navigateTo({
    //     url: '/pages/pfep/MaintenanceTemplate/Template1/Template1',
    // })
    wx.navigateTo({
      url: '/pages/pfep/MaintenanceTemplate/Template1/Template1?citg=' + this.data.citg + '&cpcl=' + this.data.cpcl + '&material_num=' + this.data.material_code,
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.row_data)

      }
    })
  },
  toTemplate2(e) {
    console.log(e.currentTarget.dataset.row_data);
    wx.navigateTo({
      url: '/pages/pfep/MaintenanceTemplate/Template1/Template1?citg=' + this.data.citg + '&cpcl=' + this.data.cpcl + '&material_num=' + this.data.material_code,
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.row_data)

      }
    })

  },
  toTemplate3() {
    wx.navigateTo({
      url: '/pages/pfep/MaintenanceTemplate/Template2/Template3',
    })

  },
  // 预览物料编码信息
  previewBtn2() { },

  // 获取--物料组、物料分类码--数据
  getMaterialCodeDatas(val) {
    let url = this.data.baseUrl + "/standard_doc_manage/query_material_num_info"
    let data = {
      material_num: val
    }
    this.post(url, data).then(res => {
      // let data_list = Object.keys(res.data)

      if (res.code == 20000) {

        this.setData({
          citg: res.data.citg,
          cpcl: res.data.cpcl,
          material_code: val

        })
        this.getTemplateDatas()
        this.getStandardDatas()
      } else {
        // wx.showToast({
        //   title: res.msg,
        //   icon: 'error'
        // })
      }
    }).catch(err => {
      console.log(err);
      // wx.showToast({
      //   title: err.msg,
      //   icon: 'error'
      // })
    })
  },

  // 获取--物料组、物料分类码--数据
  getMaterialCodeDatas1() {
    let url = this.data.baseUrl + "/standard_doc_manage/query_material_num_info"
    let data = {
      material_num: this.data.material_code
    }
    this.post(url, data).then(res => {
      // let data_list = Object.keys(res.data)

      if (res.code == 20000) {

        this.setData({
          citg: res.data.citg,
          cpcl: res.data.cpcl,

        })
        console.log("123");
        this.getTemplateDatas()
        this.getStandardDatas()
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let gs = wx.getStorageSync('company')
    this.setData({
      company: gs
    })
    if (this.data.previewImageClick == false) {
      this.getPermissionDatas()
     

    }
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})  